#!/bin/bash

echo "当前目录: $(pwd)"
echo "编译..."
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
echo "编译成功"

echo "打包..."
rm -rf ./dist
rm -rf ./dist.zip
if [ ! -d "./dist" ]; then
  mkdir ./dist
fi
cp -rf ./runtime ./dist/
cp -rf ./resource ./dist/
cp -rf ./static ./dist/
cp -rf ./views ./dist/
cp -rf ./gin-api-server ./dist/
cp -rf ./conf.yml ./dist/
mv ./dist/gin-api-server ./dist/app
rm -rf ./gin-api-server
zip -q -r dist.zip ./dist
echo "打包成功"

# 提示用户选择要上传的内容
echo "请选择要上传的内容："
echo "1. 上传 app 文件"
echo "2. 上传整个 dist 目录"
read -p "输入您的选择（1 或 2）： " choice

user="root"
remote_host=""
local_path="$(pwd)/dist"
remote_path=""
bt_project_name=""
api_sk=""
start_url="http://  /project/go/start_project"
stop_url="http://  /project/go/stop_project"

case $choice in
1)
  # 上传 app 文件
  local_file="$local_path/app"
  echo "正在上传 app 文件..."
  scp "$local_file" "$user@$remote_host:$remote_path/"
  ;;
2)
  # 上传 dist 目录
  local_dir="$local_path"
  echo "正在上传整个 dist 目录..."
  rsync -avz --progress --rsh='ssh' "$local_dir/" "$user@$remote_host:$remote_path/"
  ;;
*)
  echo "无效选择，脚本已退出。"
  exit 1
  ;;
esac

echo "上传成功"

echo "请选择要上传的内容："
echo "1. 是否为宝塔面板"
echo "2. 直接退出"
read -p "输入您的选择（1 或 2）： " isbt

case $isbt in
1) ;;

\
  2)
  echo "退出"
  exit 1
  ;;
*)
  echo "无效选择，脚本已退出。"
  exit 1
  ;;
esac

# 计算请求时间和请求令牌
request_time=$(date +%s)
request_token=$(echo -n "$request_time$(echo -n "$api_sk" | md5 | awk '{print $1}')" | md5 | awk '{print $1}')

curl --location --request POST "$stop_url" \
  --header 'User-Agent: Apifox/1.0.0 (https://www.apifox.cn)' \
  --form "request_time=\"${request_time}\"" \
  --form "request_token=\"${request_token}\"" \
  --form "data=\"{\\\"project_name\\\":\\\"${bt_project_name}\\\"}\"" \
  -s | jq

echo ""

curl --location --request POST "$start_url" \
  --header 'User-Agent: Apifox/1.0.0 (https://www.apifox.cn)' \
  --form "request_time=\"${request_time}\"" \
  --form "request_token=\"${request_token}\"" \
  --form "data=\"{\\\"project_name\\\":\\\"${bt_project_name}\\\"}\"" \
  -s | jq
